package top.kent90.common.util;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

public class VerifyCode {

	public static  String drawRandomText(int width,int height,
			 BufferedImage verifyImg) {
        Graphics2D graphics = (Graphics2D)verifyImg.getGraphics();
        graphics.setColor(Color.WHITE);//���û�����ɫ-��֤�뱳��ɫ
        graphics.fillRect(0, 0, width, height);//��䱳��
        graphics.setFont(new Font("Fixedsys", Font.BOLD, 30));
 
        //���ֺ���ĸ�����
        String baseNumLetter = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
        StringBuffer sBuffer = new StringBuffer();
        int x = 10;  //��תԭ��� x ����
        String ch = "";
        Random random = new Random();
        for(int i = 0;i < 4;i++){
                 graphics.setColor(getRandomColor());
 
            //����������ת�Ƕ�
            int degree = random.nextInt() % 30;  //�Ƕ�С��30��
            int dot = random.nextInt(baseNumLetter.length());
            ch = baseNumLetter.charAt(dot) + "";
            sBuffer.append(ch);
            //������ת
            graphics.rotate(degree * Math.PI / 120, x, 45);
            graphics.drawString(ch, x, 45);
            //������ת
            graphics.rotate(-degree * Math.PI / 120, x, 45);
            x += 30;
        }
 
        //��������
        for (int i = 0; i <6; i++) {
            // ���������ɫ
           graphics.setColor(getRandomColor());
            // �������
           graphics.drawLine(random.nextInt(width), random.nextInt(height),
        		   random.nextInt(width), random.nextInt(height));
        }
 
        //������
        for(int i=0;i<30;i++){
                 int x1 = random.nextInt(width);
                 int y1 = random.nextInt(height);
                 graphics.setColor(getRandomColor());
                 graphics.fillRect(x1, y1, 2,2);
        }
        return sBuffer.toString();
    }

	/**
	 * ���ȡɫ
	 */
	private static Color getRandomColor() {
		Random ran = new Random();
		Color color = new Color(ran.nextInt(256),ran.nextInt(256), ran.nextInt(256));
		return color;
	}
	
	/**
	 *    �����֤��
	 * @param request
	 * @param response
	 * @param width
	 * @param height
	 */
	public static void exportImg(HttpServletRequest request,HttpServletResponse response,
			    int width,int height) {
		OutputStream os = null;
		try {
			BufferedImage verifyImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
			//���ɶ�Ӧ��ߵĳ�ʼͼƬ
			String randomText = VerifyCode.drawRandomText(width,height,verifyImg);
			//������������֤���ַ���������㣬�����ߣ�����ֵΪ��֤���ַ�                   
			//sevletjsp���м���������pageContext-request-session-application			        
			request.getSession().setAttribute("verifyCode", randomText);
			//���������������
			response.setDateHeader("Expires", -1);//IE
		    response.setHeader("Cache-Control", "no-cache");
		    response.setHeader("Pragma", "no-cache");
		    
			response.setContentType("image/png");//����������Ӧ��������ΪͼƬ������ǰ̨��ʶ��
	        os = response.getOutputStream(); //��ȡ�ļ������    
	        
	        ImageIO.write(verifyImg,"png",os);//���ͼƬ��
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				os.flush();
				os.close();//�ر���
			} catch (IOException e) {
				e.printStackTrace();
		   }
		}
	}
}